package com.lancoo.znbkxx.base;

import android.app.Application;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by TangJun on 2019/2/16
 */
public abstract class ApplicationBase extends Application {
    private List<Class<? extends IApplicationStateCall>> registers = new ArrayList<>();
    private List<IApplicationStateCall> calls = new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        onRegisterInit();
        onCreateInitCall();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        onLowMemoryCall();
    }

    protected void onCreateInitCall() {
        for (Class<? extends IApplicationStateCall> register : registers) {
            try {
                IApplicationStateCall applicationStateCall = register.newInstance();
                calls.add(applicationStateCall);
                applicationStateCall.onCreateInit(this);
            } catch (Exception e) {
                //Logger.e("初始化异常：" + e.toString());
            }
        }

    }

    protected void onLowMemoryCall() {
        for (IApplicationStateCall call : calls) {
            try {
                call.onLowMemory(this);
            } catch (Exception e) {
               // Logger.e("回调化异常：" + e.toString());
            }
        }
    }

    protected abstract void onRegisterInit();

    protected void registerInitCall(Class<? extends IApplicationStateCall> intiClass) {
        registers.add(intiClass);
    }





}
